package com.cx.controller.wjr;

import com.cx.pojo.Site;
import com.cx.pojo.SiteExample;
import com.cx.service.wjr.SiteService;
import com.cx.vo.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminSiteController {
    @Autowired
    SiteService adminSiteService;
    @RequestMapping("addSite")
    @ResponseBody
    public ResponseResult add(ResponseResult responseResult, Model model, Site site){
        int siteNum =  adminSiteService.addCourier(site);
        if(siteNum > 0){
            responseResult.setMessage("添加成功");
            responseResult.setCode(0);
            return responseResult;
        }else{
            responseResult.setCode(1);
            responseResult.setMessage("添加失败");
            return responseResult;
        }
    }

    @RequestMapping("adminSiteList")
    @ResponseBody
    public ResponseResult list(Integer pageNum,ResponseResult responseResult,Model model, SiteExample siteExample){
        PageHelper.startPage(pageNum, 5);
        List<Site> siteList =  adminSiteService.selectByExample(siteExample);
        PageInfo pageInfo = new PageInfo(siteList,5);
        siteList.forEach(System.out::println);
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @RequestMapping("adminUpdateSite")
    @ResponseBody
    public ResponseResult update(ResponseResult responseResult,Model model,Site site, SiteExample siteExample){
        if(null != site.getAreaId()){
            siteExample.createCriteria().andAreaIdEqualTo(site.getAreaId());
        }
        if(null != site.getCityId()){
            siteExample.createCriteria().andCityIdEqualTo(site.getCityId());
        }
        if(null != site.getSiteName() && site.getSiteName() != ""){
            siteExample.createCriteria().andSiteNameEqualTo(site.getSiteName());
        }
        if(null != site.getSitePhone() && site.getSitePhone() !=""){
            siteExample.createCriteria().andSitePhoneEqualTo(site.getSitePhone());
        }

        int courierList =  adminSiteService.update(site,siteExample);
        //courierList.forEach(System.out::println);
        responseResult.setData(courierList);
        return responseResult;
    }

    @RequestMapping("adminSelectSite")
    @ResponseBody
    public ResponseResult select(ResponseResult responseResult, Model model, Site site, SiteExample siteExample){
        if(null != site.getSiteId()){
            siteExample.createCriteria().andSiteIdEqualTo(site.getSiteId());
        }
//        siteExample.createCriteria().andSitePhoneEqualTo(site.getSitePhone());
        List<Site> siteNum = adminSiteService.selectByExample(siteExample);

        siteNum.forEach(System.out::println);
        System.out.println(siteNum);
        responseResult.setData(siteNum);
        if(siteNum.size() > 0){
            responseResult.setMessage("查询成功");
            return responseResult;
        }
        responseResult.setMessage("查询失败");
        return responseResult;
    }

    @RequestMapping("adminSiteDelete")
    @ResponseBody
    public ResponseResult userDelete(ResponseResult responseResult,SiteExample siteExample,Site site,Model model){
        if(null != site){
            int count =  adminSiteService.delete(siteExample);
            if(count>0){
                responseResult.setMessage("删除成功");
                return responseResult;
            }else{
                responseResult.setMessage("删除失败");
                return responseResult;
            }
        }
        return responseResult;

    }

}
